package com.corex.webservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.StrictMode;

import com.classapp.login.IRequest;
import com.classapp.login.LoginBeanMobile;
import com.corex.staticclasses.StaticData;
import com.google.gson.Gson;

public abstract class BaseService{
	private Object request;
	private Object requestPayload;
	private String url;
	private Class responceClass;
	private LoginBeanMobile loginBeanMobile;
	private IRequest iRequest;
	
	private Object getRequest() {
		return this.request;
	}

	private void setRequest(Object request) {
		this.request = request;

	}

	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;

	}

	public Class getResponceClass() {
		return responceClass;
	}

	public void setResponceClass(Class responceClass) {
		this.responceClass = responceClass;
	}
	
	

	public LoginBeanMobile getLoginBeanMobile() {
		return loginBeanMobile;
	}

	public void setLoginBeanMobile(LoginBeanMobile loginBeanMobile) {
		this.loginBeanMobile = loginBeanMobile;
		getiRequest().setLoginBeanMobile(loginBeanMobile);
	}

	public IRequest getiRequest() {
		if(null==iRequest){
			iRequest = new IRequest();
		}
		return iRequest;
	}

	public void setiRequest(IRequest iRequest) {
		this.iRequest = iRequest;
	}
	
	public Object getRequestPayload() {
		return requestPayload;
	}

	public void setRequestPayload(Object requestPayload) {
		this.requestPayload = requestPayload;
	}

	public Object execute() throws Exception {
		//This is static bean loads at login screen 
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy); 
		
		loginBeanMobile = StaticData.loginBeanMobile;
		
		IRequest request = new IRequest();
		request.setLoginBeanMobile(loginBeanMobile);
		request.setObject(requestPayload);
		setRequest(request);
		
		
		if (null == this.url || null == this.request || null == this.responceClass) {
			throw new Exception(
					"URL,request and responceClass must be initialised");
		}
		Object object = getRequest();
		Gson gson = new Gson();
		String requestString = gson.toJson(request);
		
		String respStr = callService(this.url, requestString);
		Object response = gson.fromJson(respStr, this.responceClass);
		return response;
	}

	private String callService(String urlstr, String requestJson) {

		/*
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);
		*/
		
		String responceOutput = "";
		try {
			URL url = new URL(urlstr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/json");
			conn.setRequestProperty("Accept", "application/json");

			OutputStream os = conn.getOutputStream();
			os.write(requestJson.getBytes());
			os.flush();

			/*
			 * switch (method) { case WebServiceCalls.GET:
			 * conn.setRequestMethod("GET"); conn.setRequestProperty("Accept",
			 * "application/json"); break;
			 * 
			 * case WebServiceCalls.POST: conn.setRequestMethod("POST");
			 * conn.setRequestProperty("Content-Type", "application/json");
			 * conn.setRequestProperty("Accept", "application/json");
			 * conn.setDoOutput(true);
			 * 
			 * OutputStream os = conn.getOutputStream();
			 * os.write(requestJson.getBytes()); os.flush(); break;
			 * 
			 * default: break; }
			 */
			if (conn.getResponseCode() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ conn.getResponseCode());
			} else {

				BufferedReader br = new BufferedReader(new InputStreamReader(
						(conn.getInputStream())));

				String output;
				while ((output = br.readLine()) != null) {

					responceOutput = responceOutput + output;
				}
			}
			conn.disconnect();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return responceOutput;

	}

}
